﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Commmon.UI;
using System.Data;
using System.ComponentModel;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList.Nodes;
using MM.Common.Base;
using System.Reflection;
using MM.Common.Setup;
using DevExpress.XtraEditors;
using MM.Common.DBBase;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Controls;
using MM.Common.UI.Controls;

namespace MM.Common.Support
{
    public partial class FrmFormAdd : FrmBase
    {
        private DataTable dtFunction;
        private TreeListNode moduleNode;
        public FrmFormAdd()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FORM_CODE", typeof(string));
            dt.Columns.Add("FORM_NAME", typeof(string));
            dt.Columns.Add("OBJECT_NAME", typeof(string));
            dt.Columns.Add("FULL_NAME", typeof(string));
            string str = this.moduleNode.GetValue("OBJECT_NAME").ToString();
            string assemblyFile = SystemInfo.RunPath + str + ".dll";
            try
            {
                Assembly assembly = Assembly.LoadFrom(assemblyFile);
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsClass)
                    {
                        for (Type type2 = type.BaseType; type2 != null; type2 = type2.BaseType)
                        {
                            if (type2.Name == "FrmBase")
                            {
                                try
                                {
                                    FrmBase base2 = (FrmBase)assembly.CreateInstance(type.FullName);
                                    if (!string.IsNullOrEmpty(base2.FormCode) && !this.CheckFormExist(base2.FormCode))
                                    {
                                        int num = type.FullName.LastIndexOf('.');
                                        DataRow row = dt.NewRow();
                                        row["FORM_CODE"] = base2.FormCode;
                                        row["FORM_NAME"] = base2.Text;
                                        row["OBJECT_NAME"] = type.FullName.Substring(num + 1);
                                        row["FULL_NAME"] = type.FullName;
                                        dt.Rows.Add(row);
                                    }
                                    base2.Dispose();
                                }
                                catch
                                {
                                }
                                break;
                            }
                        }
                    }
                }
                if (dt.Rows.Count > 0)
                {
                    dt.DefaultView.Sort = "FORM_CODE";
                    FrmSelectFromGrid grid = new FrmSelectFromGrid(dt, new string[] { "代码", "名称", "对象", "全名" }, new int[] { 20, 40, 40, 0 }, "选择窗口");
                    if (grid.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        int choiceRow = grid.ChoiceRow;
                        this.txtFormCode.Text = dt.Rows[choiceRow]["FORM_CODE"].ToString();
                        this.txtFormName.Text = dt.Rows[choiceRow]["FORM_NAME"].ToString();
                        this.txtObjectName.Text = dt.Rows[choiceRow]["OBJECT_NAME"].ToString();
                        this.dtFunction.Rows.Clear();
                        FrmBase ctl = (FrmBase)Assembly.LoadFrom(assemblyFile).CreateInstance(dt.Rows[choiceRow]["FULL_NAME"].ToString());
                        this.GetFunctionPoint(ctl);
                    }
                }
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show("出错：" + exception.Message, "提示");
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string text = this.txtFormCode.Text;
            string paramValue = this.txtFormName.Text;
            string str3 = this.txtObjectName.Text;
            string str4 = this.moduleNode.GetValue("CODE").ToString();
            string[] commandtexts = new string[this.dtFunction.Rows.Count + 1];
            DataParamCol[] parms = new DataParamCol[this.dtFunction.Rows.Count + 1];
            commandtexts[0] = "INSERT INTO KTSYS_FORM (FORM_CODE ,FORM_NAME ,OBJECT_NAME ,MODULE_CODE ,DESCRIBE) \r\n VALUES (:formcode ,:formname ,:objectname ,:modulecode ,:describe) ";
            parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("formcode", text), new InputDataParam("formname", paramValue), new InputDataParam("objectname", str3), new InputDataParam("modulecode", str4), new InputDataParam("describe", this.txtDescribe.Text) });
            for (int i = 0; i < this.dtFunction.Rows.Count; i++)
            {
                commandtexts[i + 1] = "UPDATE MXHLY_SYS_FUNCTION_POINT SET FUNCTION_POINT_NAME = :fname, FORM_CODE = :formcode, DEFAULT_PROPERTY = :fproperty \r\n   WHERE FUNCTION_POINT_CODE = :fcode ";
                parms[i + 1] = new DataParamCol(new DataParam[] { new InputDataParam("fname", this.dtFunction.Rows[i]["FUNCTION_POINT_NAME"].ToString()), new InputDataParam("formcode", text), new InputDataParam("fproperty", this.dtFunction.Rows[i]["PROPERTY"].ToString()), new InputDataParam("fcode", this.dtFunction.Rows[i]["FUNCTION_POINT_CODE"].ToString()) });
            }
            if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
            {
                TreeListNode parentNode = this.moduleNode.TreeList.AppendNode(new object[] { text, paramValue, str3, " " }, this.moduleNode);
                parentNode.ImageIndex = 2;
                parentNode.SelectImageIndex = 2;
                this.moduleNode.TreeList.FocusedNode = parentNode;
                for (int j = 0; j < this.dtFunction.Rows.Count; j++)
                {
                    TreeListNode node2 = parentNode.TreeList.AppendNode(this.dtFunction.Rows[j], parentNode);
                    node2.ImageIndex = 3;
                    node2.SelectImageIndex = 3;
                }
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
                base.Close();
            }
            else
            {
                XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        protected bool CheckFormExist(string code)
        {
            for (int i = 0; i < this.moduleNode.Nodes.Count; i++)
            {
                if (this.moduleNode.Nodes[i].GetValue("CODE").ToString() == code)
                {
                    return true;
                }
            }
            return false;
        }
        protected void GetFunctionPoint(Control ctl)
        {
            int count = ctl.Controls.Count;
            for (int i = 0; i < count; i++)
            {
                if (ctl.Controls[i] is IControlProperty)
                {
                    PropertyInfo property = ctl.Controls[i].GetType().GetProperty("ControlCode");
                    if (property != null)
                    {
                        object obj2 = property.GetValue(ctl.Controls[i], null);
                        string str = "";
                        if (obj2 != null)
                        {
                            str = obj2.ToString();
                        }
                        if (str != "")
                        {
                            obj2 = ctl.Controls[i].GetType().GetProperty("ControlName").GetValue(ctl.Controls[i], null);
                            string str2 = "";
                            if (obj2 != null)
                            {
                                str2 = obj2.ToString();
                            }
                            DataRow row = this.dtFunction.NewRow();
                            row["FUNCTION_POINT_CODE"] = str;
                            row["FUNCTION_POINT_NAME"] = str2;
                            row["PROPERTY"] = "W";
                            this.dtFunction.Rows.Add(row);
                            continue;
                        }
                    }
                }
                this.GetFunctionPoint(ctl.Controls[i]);
            }
        }
        public bool SetParameter(TreeListNode node)
        {
            if (this.Init())
            {
                this.moduleNode = node;
                DataTable table = new DataTable();
                table.Columns.Add("PROPERTY_CODE", typeof(string));
                table.Columns.Add("PROPERTY_NAME", typeof(string));
                table.Rows.Add(new object[] { "H", "不可视" });
                table.Rows.Add(new object[] { "R", "只读" });
                table.Rows.Add(new object[] { "W", "全部" });
                this.riProperty.ValueMember = "PROPERTY_CODE";
                this.riProperty.DisplayMember = "PROPERTY_NAME";
                this.riProperty.DataSource = table;
                this.dtFunction = new DataTable();
                this.dtFunction.Columns.Add("FUNCTION_POINT_CODE", typeof(string));
                this.dtFunction.Columns.Add("FUNCTION_POINT_NAME", typeof(string));
                this.dtFunction.Columns.Add("OBJECT_NAME", typeof(string));
                this.dtFunction.Columns.Add("PROPERTY", typeof(string));
                this.gvFunction.GridControl.DataSource = this.dtFunction;
                return true;
            }
            return false;
        }
    }
}
